/**
 * Copyright (c) Islon Scherer 2010.
 * Based on clojure.lang.AFn from Rich Hickey.
 */

package progma.lang;

import java.util.Collection;

import progma.utils.Util;

public abstract class PFunction implements IPFunction {

	public Object call() throws Exception {
		return invoke();
	}

	public void run() {
		try {
			invoke();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public Object invoke() throws Exception {
		return throwArityException(0);
	}

	public Object invoke(Object arg1) throws Exception {
		return throwArityException(1);
	}

	public Object invoke(Object arg1, Object arg2) throws Exception {
		return throwArityException(2);
	}

	public Object invoke(Object arg1, Object arg2, Object arg3)
			throws Exception {
		return throwArityException(3);
	}

	public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4)
			throws Exception {
		return throwArityException(4);
	}

	public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4,
			Object arg5) throws Exception {
		return throwArityException(5);
	}

	public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4,
			Object arg5, Object arg6) throws Exception {
		return throwArityException(6);
	}

	public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4,
			Object arg5, Object arg6, Object arg7) throws Exception {
		return throwArityException(7);
	}

	public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4,
			Object arg5, Object arg6, Object arg7, Object arg8)
			throws Exception {
		return throwArityException(8);
	}

	public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4,
			Object arg5, Object arg6, Object arg7, Object arg8, Object arg9)
			throws Exception {
		return throwArityException(9);
	}

	public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4,
			Object arg5, Object arg6, Object arg7, Object arg8, Object arg9,
			Object arg10) throws Exception {
		return throwArityException(10);
	}

	public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4,
			Object arg5, Object arg6, Object arg7, Object arg8, Object arg9,
			Object arg10, Object arg11) throws Exception {
		return throwArityException(11);
	}

	public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4,
			Object arg5, Object arg6, Object arg7, Object arg8, Object arg9,
			Object arg10, Object arg11, Object arg12) throws Exception {
		return throwArityException(12);
	}

	public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4,
			Object arg5, Object arg6, Object arg7, Object arg8, Object arg9,
			Object arg10, Object arg11, Object arg12, Object arg13)
			throws Exception {
		return throwArityException(13);
	}

	public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4,
			Object arg5, Object arg6, Object arg7, Object arg8, Object arg9,
			Object arg10, Object arg11, Object arg12, Object arg13, Object arg14)
			throws Exception {
		return throwArityException(14);
	}

	public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4,
			Object arg5, Object arg6, Object arg7, Object arg8, Object arg9,
			Object arg10, Object arg11, Object arg12, Object arg13,
			Object arg14, Object arg15) throws Exception {
		return throwArityException(15);
	}

	public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4,
			Object arg5, Object arg6, Object arg7, Object arg8, Object arg9,
			Object arg10, Object arg11, Object arg12, Object arg13,
			Object arg14, Object arg15, Object arg16) throws Exception {
		return throwArityException(16);
	}

	public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4,
			Object arg5, Object arg6, Object arg7, Object arg8, Object arg9,
			Object arg10, Object arg11, Object arg12, Object arg13,
			Object arg14, Object arg15, Object arg16, Object arg17)
			throws Exception {
		return throwArityException(17);
	}

	public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4,
			Object arg5, Object arg6, Object arg7, Object arg8, Object arg9,
			Object arg10, Object arg11, Object arg12, Object arg13,
			Object arg14, Object arg15, Object arg16, Object arg17, Object arg18)
			throws Exception {
		return throwArityException(18);
	}

	public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4,
			Object arg5, Object arg6, Object arg7, Object arg8, Object arg9,
			Object arg10, Object arg11, Object arg12, Object arg13,
			Object arg14, Object arg15, Object arg16, Object arg17,
			Object arg18, Object arg19) throws Exception {
		return throwArityException(19);
	}

	public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4,
			Object arg5, Object arg6, Object arg7, Object arg8, Object arg9,
			Object arg10, Object arg11, Object arg12, Object arg13,
			Object arg14, Object arg15, Object arg16, Object arg17,
			Object arg18, Object arg19, Object arg20) throws Exception {
		return throwArityException(20);
	}

	public Object invoke(Object arg1, Object arg2, Object arg3, Object arg4,
			Object arg5, Object arg6, Object arg7, Object arg8, Object arg9,
			Object arg10, Object arg11, Object arg12, Object arg13,
			Object arg14, Object arg15, Object arg16, Object arg17,
			Object arg18, Object arg19, Object arg20, Object... args)
			throws Exception {
		return throwArityException(21);
	}

	@SuppressWarnings("rawtypes")
	public Object apply(Collection args) throws Exception {
		return applyToHelper(this, args.toArray());
	}

	public Object apply(Object[] args) throws Exception {
		return applyToHelper(this, args);
	}

	static public Object applyToHelper(IPFunction ipf, Object[] args)
			throws Exception {
		switch (args.length) {
		case 0:
			return ipf.invoke();
		case 1:
			Object a1 = args[0];
			return ipf.invoke(a1);
		case 2:
			return ipf.invoke(args[0], args[1]);
		case 3:
			return ipf.invoke(args[0], args[1], args[2]);
		case 4:
			return ipf.invoke(args[0], args[1], args[2], args[3]);
		case 5:
			return ipf.invoke(args[0], args[1], args[2], args[3], args[4]);
		case 6:
			return ipf.invoke(args[0], args[1], args[2], args[3], args[4],
					args[5]);
		case 7:
			return ipf.invoke(args[0], args[1], args[2], args[3], args[4],
					args[5], args[6]);
		case 8:
			return ipf.invoke(args[0], args[1], args[2], args[3], args[4],
					args[5], args[6], args[7]);
		case 9:
			return ipf.invoke(args[0], args[1], args[2], args[3], args[4],
					args[5], args[6], args[7], args[8]);
		case 10:
			return ipf.invoke(args[0], args[1], args[2], args[3], args[4],
					args[5], args[6], args[7], args[8], args[9]);
		case 11:
			return ipf.invoke(args[0], args[1], args[2], args[3], args[4],
					args[5], args[6], args[7], args[8], args[9], args[10]);
		case 12:
			return ipf.invoke(args[0], args[1], args[2], args[3], args[4],
					args[5], args[6], args[7], args[8], args[9], args[10],
					args[11]);
		case 13:
			return ipf.invoke(args[0], args[1], args[2], args[3], args[4],
					args[5], args[6], args[7], args[8], args[9], args[10],
					args[11], args[12]);
		case 14:
			return ipf.invoke(args[0], args[1], args[2], args[3], args[4],
					args[5], args[6], args[7], args[8], args[9], args[10],
					args[11], args[12], args[13]);
		case 15:
			return ipf.invoke(args[0], args[1], args[2], args[3], args[4],
					args[5], args[6], args[7], args[8], args[9], args[10],
					args[11], args[12], args[13], args[14]);
		case 16:
			return ipf.invoke(args[0], args[1], args[2], args[3], args[4],
					args[5], args[6], args[7], args[8], args[9], args[10],
					args[11], args[12], args[13], args[14], args[15]);
		case 17:
			return ipf.invoke(args[0], args[1], args[2], args[3], args[4],
					args[5], args[6], args[7], args[8], args[9], args[10],
					args[11], args[12], args[13], args[14], args[15], args[16]);
		case 18:
			return ipf.invoke(args[0], args[1], args[2], args[3], args[4],
					args[5], args[6], args[7], args[8], args[9], args[10],
					args[11], args[12], args[13], args[14], args[15], args[16],
					args[17]);
		case 19:
			return ipf.invoke(args[0], args[1], args[2], args[3], args[4],
					args[5], args[6], args[7], args[8], args[9], args[10],
					args[11], args[12], args[13], args[14], args[15], args[16],
					args[17], args[18]);
		case 20:
			return ipf.invoke(args[0], args[1], args[2], args[3], args[4],
					args[5], args[6], args[7], args[8], args[9], args[10],
					args[11], args[12], args[13], args[14], args[15], args[16],
					args[17], args[18], args[19]);
		default:
			return ipf.invoke(args[0], args[1], args[2], args[3], args[4],
					args[5], args[6], args[7], args[8], args[9], args[10],
					args[11], args[12], args[13], args[14], args[15], args[16],
					args[17], args[18], args[19], Util.restOfArray(args, 20));
		}
	}

	public Object throwArityException(int n) {
		String name = getClass().getSimpleName();
		throw new IllegalArgumentException("Wrong number of args (" + n
				+ ") passed to: " + name);
	}
}
